package com.yamvc.net.srp
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class SRPConnector
	{
		private var _client:Object;
		public function get client():Object{
			return _client;
		}
		
		public function set client(value:Object):void{
			_client = value;
		}
		
		private var _servise:HTTPService;
		public function SRPConnector(url:String=null)
		{
			_servise = new HTTPService(url);
			_servise.useProxy=false;
			_servise.url=url;
			_servise.method="POST";
			_servise.addEventListener(ResultEvent.RESULT,resultHandler);
			_servise.addEventListener(FaultEvent.FAULT,faultHandler);
		}
		
		public function faultHandler(e:FaultEvent):void{
			trace(e.fault.faultString);
		}
		
		public function resultHandler(e:ResultEvent):void{
			var result:Object = e.result;
			var command = result.command.name;
			var params = result.command.params;
			if(params==null) params={};
			_client[command].apply(_client,[params]);
		}
		
		public function call(name:String,params:Object){
			params.commandName = name;
			_servise.send(params);
		}

	}
}